<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	<xsl:include href="../../admin.xsl" />
	<xsl:include href="../../common/form.xsl" />
	<xsl:include href="../../cp/path.xsl" />
	
	<!-- Запускаем рендер страницы -->
	<xsl:template match="/data">
		<html>
		
		<head>
			<title>
				<xsl:value-of select="$pageTitle" />
			</title>
			<xsl:call-template name="meta-head" />
			<link rel="stylesheet" href="{$baseURI}/css/admin/forms.css" type="text/css" media="all" />
		</head>
		<body>
			<xsl:call-template name="admin-header" />
			
			<div id="blocks">
				<div id="blocks_inner" />
			</div>
			
			<div id="content">
				<div id="content_inner">
					<xsl:apply-templates select="response" />
				</div>
			</div>
			
			<xsl:call-template name="footer" />
		</body>
		</html>
	</xsl:template>
	
	<xsl:template match="response">
		<div id="path">
			<xsl:apply-templates select="document('xmodule://cp/getPath/0;system;types_connections')/data/response" mode="cp-path">
				<xsl:with-param name="activeLink">1</xsl:with-param>
			</xsl:apply-templates>
		</div>
		
		<h1>
			<xsl:if test="$method = 'add_types_connection'">
				<xsl:text>Новая связь</xsl:text>
			</xsl:if>
			<xsl:if test="$method = 'edit_types_connection'">
				<xsl:text>Редактирование связи</xsl:text>
			</xsl:if>
		</h1>
		
		<form action="{$baseURI}/admin/system/add_types_connection/save" method="post">
			<xsl:if test="$method = 'edit_types_connection'">
				<xsl:attribute name="action">
					<xsl:value-of select="concat($baseURI, '/admin/system/edit_types_connection/', $formPrefix, '/save')" />
				</xsl:attribute>
			</xsl:if>
			
			<xsl:apply-templates select="form/field[@name='parent_type_id']" />
			
			<xsl:apply-templates select="form/field[@name='parent_field_id']">
				<xsl:with-param name="selectedType">
					<xsl:choose>
						<xsl:when test="form/field[@name='parent_type_id']/value = ''">
							<xsl:text>0</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="form/field[@name='parent_type_id']/value" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
			</xsl:apply-templates>
			
			<xsl:apply-templates select="form/field[@name='child_type_id']" />
			
			<xsl:apply-templates select="form/field[@name='child_rel_field_id']">
				<xsl:with-param name="selectedType">
					<xsl:choose>
						<xsl:when test="form/field[@name='child_type_id']/value = ''">
							<xsl:text>0</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="form/field[@name='child_type_id']/value" />
						</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
			</xsl:apply-templates>
			
			<xsl:apply-templates select="form/field[@name='connection_type']" />
			
			<p>
				<input type="submit" value="Сохранить" />
			</p>
		</form>
		
		<script type="text/javascript">
			function reloadFields (typeId, inputId) {
				if (!typeId) {
					return false;
				}
				
				$.ajax ({
					url: baseURI + '/admin/system/ajax_get_type_fields/' + typeId,
					dataType: 'xml',
					type: 'get',
					error: function() { alert('Произошла ошибка!') },
					success: function(xml) {
						$('#'+inputId+' option[value!=0]').remove();
						
						var fields = $(xml).find('field').each (function() {
							$("<option></option>")
								.attr('value', $(this).find('id').text())
								.text($(this).find('title').text())
								.appendTo('#'+inputId);
						});
						
						$('#'+inputId).attr('disabled',false);
					}
				});
			}
		</script>
	</xsl:template>
	
																						<!-- CHILD -->
	
	<xsl:template match="form/field[@name='parent_type_id']">
		<xsl:param name="value" select="value" />
		
		<div class="form_field">
			<div class="form_field_label">
				<label for="field_{@id}">
					<xsl:text>Родительский тип данных:</xsl:text>
				</label>
			</div>
			<div class="form_field_input">
				<select id="field_{@id}" class="form_input_select" name="data[{$formPrefix}][{@name}]" onchange="reloadFields(this.value, 'parent_fields_list')">
					<xsl:if test="$method = 'edit_types_connection'">
						<xsl:attribute name="disabled"><xsl:text>disabled</xsl:text></xsl:attribute>
					</xsl:if>
					
					<option value="0">-- выберите</option>					
					
					<xsl:for-each select="document('xmodule://system/getTypes')/data/response/object[@type='explay_type']">
						<option>
							<xsl:attribute name="value">
								<xsl:value-of select="@id" />
							</xsl:attribute>
							<xsl:if test="$value = @id">
								<xsl:attribute name="selected">
									<xsl:text>selected</xsl:text>
								</xsl:attribute>
							</xsl:if>
							
							<xsl:value-of select="property[@name='title']/value" />
						</option>
					</xsl:for-each>				
				</select>
				<xsl:if test="error != ''">
					<br /><span class="form_field_error"><xsl:value-of select="error" /></span>
				</xsl:if>
				<xsl:if test="help != ''">
					<br /><span class="form_field_help"><xsl:value-of select="help" /></span>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
	<xsl:template match="form/field[@name='parent_field_id']">
		<xsl:param name="selectedType" />
		<xsl:param name="value" select="value" />
		
		<div class="form_field">
			<div class="form_field_label">
				<label for="field_{@id}">
					<xsl:text>Родительское поле:</xsl:text>
				</label>
			</div>
			<div class="form_field_input">
				<select id="parent_fields_list" class="form_input_select" name="data[{$formPrefix}][{@name}]">
					<xsl:if test="$selectedType = 0">
						<xsl:attribute name="disabled"><xsl:text>disabled</xsl:text></xsl:attribute>
					</xsl:if>
					
					<option value="0">id</option>
					
					<xsl:if test="$selectedType != 0">
						<xsl:for-each select="document(concat('xmodule://system/getTypeFields/',$selectedType))/data/response/object[@type='explay_field']">
							<option>
								<xsl:attribute name="value">
									<xsl:value-of select="@id" />
								</xsl:attribute>
								<xsl:if test="$value = @id">
									<xsl:attribute name="selected">
										<xsl:text>selected</xsl:text>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:value-of select="property[@name='title']/value" />
							</option>
						</xsl:for-each>	
					</xsl:if>
				</select>
				<xsl:if test="error != ''">
					<br /><span class="form_field_error"><xsl:value-of select="error" /></span>
				</xsl:if>
				<xsl:if test="help != ''">
					<br /><span class="form_field_help"><xsl:value-of select="help" /></span>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
																						<!-- CHILD -->
	
	<xsl:template match="form/field[@name='child_type_id']">
		<xsl:param name="value" select="value" />
		
		<div class="form_field">
			<div class="form_field_label">
				<label for="field_{@id}">
					<xsl:text>Дочерний тип данных:</xsl:text>
				</label>
			</div>
			<div class="form_field_input">
				<select id="field_{@id}" class="form_input_select" name="data[{$formPrefix}][{@name}]" onchange="reloadFields(this.value, 'child_fields_list')">
					<xsl:if test="$method = 'edit_types_connection'">
						<xsl:attribute name="disabled"><xsl:text>disabled</xsl:text></xsl:attribute>
					</xsl:if>
					
					<option value="0">-- выберите</option>					
					
					<xsl:for-each select="document('xmodule://system/getTypes')/data/response/object[@type='explay_type']">
						<option>
							<xsl:attribute name="value">
								<xsl:value-of select="@id" />
							</xsl:attribute>
							<xsl:if test="$value = @id">
								<xsl:attribute name="selected">
									<xsl:text>selected</xsl:text>
								</xsl:attribute>
							</xsl:if>
							
							<xsl:value-of select="property[@name='title']/value" />
						</option>
					</xsl:for-each>				
				</select>
				<xsl:if test="error != ''">
					<br /><span class="form_field_error"><xsl:value-of select="error" /></span>
				</xsl:if>
				<xsl:if test="help != ''">
					<br /><span class="form_field_help"><xsl:value-of select="help" /></span>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
	<xsl:template match="form/field[@name='child_rel_field_id']">
		<xsl:param name="selectedType" />
		<xsl:param name="value" select="value" />
		
		<div class="form_field">
			<div class="form_field_label">
				<label for="field_{@id}">
					<xsl:text>Дочернее зависимое поле:</xsl:text>
				</label>
			</div>
			<div class="form_field_input">
				<select id="child_fields_list" class="form_input_select" name="data[{$formPrefix}][{@name}]">
					<xsl:if test="$selectedType = 0">
						<xsl:attribute name="disabled"><xsl:text>disabled</xsl:text></xsl:attribute>
					</xsl:if>
					
					<option value="0">-- выберите</option>
					
					<xsl:if test="$selectedType != 0">
						<xsl:for-each select="document(concat('xmodule://system/getTypeFields/',$selectedType))/data/response/object[@type='explay_field']">
							<option>
								<xsl:attribute name="value">
									<xsl:value-of select="@id" />
								</xsl:attribute>
								<xsl:if test="$value = @id">
									<xsl:attribute name="selected">
										<xsl:text>selected</xsl:text>
									</xsl:attribute>
								</xsl:if>
								
								<xsl:value-of select="property[@name='title']/value" />
							</option>
						</xsl:for-each>	
					</xsl:if>
				</select>
				<xsl:if test="error != ''">
					<br /><span class="form_field_error"><xsl:value-of select="error" /></span>
				</xsl:if>
				<xsl:if test="help != ''">
					<br /><span class="form_field_help"><xsl:value-of select="help" /></span>
				</xsl:if>
			</div>
		</div>
	</xsl:template>
	
	<xsl:template match="form/field[@name='connection_type']">
		<xsl:param name="selectedType" />
		<xsl:param name="value" select="value" />
		
		<div class="form_field">
			<div class="form_field_label">
				<label for="field_{@id}">
					<xsl:value-of select="title" />
					<xsl:text>:</xsl:text>
				</label>
			</div>
			<div class="form_field_input">
				<select id="field_{@id}" class="form_input_select" name="data[{$formPrefix}][{@name}]">
					<option value="">один ко многим</option>
					<option value="1">
						<xsl:if test="value = 1">
							<xsl:attribute name="selected">selected</xsl:attribute>
						</xsl:if>
						<xsl:text>один к одному</xsl:text>
					</option>
				</select>
				<xsl:if test="error != ''">
					<br /><span class="form_field_error"><xsl:value-of select="error" /></span>
				</xsl:if>
				<xsl:if test="help != ''">
					<br /><span class="form_field_help"><xsl:value-of select="help" /></span>
				</xsl:if>
			</div>
		</div>
	</xsl:template>

	
</xsl:stylesheet>